<?php
/**
 * Created by PhpStorm.
 * User: sunyang
 * Date: 2017/1/19
 * Time: 15:12
 */

function p($arr,$is_not_exit=0){
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
    if(!$is_not_exit){
        exit;
    }

}
//实例化模型
function MM($model_name, $params = array())
{
    static $models = array();

    if (!isset($models[$model_name]))
    {
        $model_file = ROOTPATH . '/model/' . $model_name . '.model.php';
        if (!is_file($model_file))
        {
            /* 不存在该文件，则无法获取模型 */
            return false;
        }
        include_once($model_file);



        $model_name = ucfirst($model_name) . 'Model';

        $models[$model_name] = new $model_name($params);
    }

    return $models[$model_name];
}
//实例化类
function ML($model_name, $params = array())
{
    static $models = array();

    if (!isset($models[$model_name]))
    {
        $model_file = ROOTPATH . '/lib/' . $model_name . '.lib.php';
        if (!is_file($model_file))
        {
            /* 不存在该文件，则无法获取模型 */
            return false;
        }
        include_once($model_file);

        $model_name = ucfirst($model_name) . 'Lib';

        $models[$model_name] = new $model_name($params);
    }

    return $models[$model_name];
}
//UTF-8转GBK
function UTF82GBK($data)
{
    if(is_array($data)){
        foreach($data as $k=>$v){
            $data[$k] = UTF82GBK($v);
        }
    }else{
        $data = iconv('UTF-8','GBK',$data);
    }
    return $data;
}

//GBK 转 UTF-8
function GBK2UTF8($data)
{
    if(is_array($data)){
        foreach($data as $k=>$v){
            $data[$k] = GBK2UTF8($v);
        }
    }else{
        $data = iconv('GBK','UTF-8',$data);
    }
    return $data;
}
//对象转数组
function objectToArray($object)
{
    $result = array();
    $object = is_object($object) ? get_object_vars($object) : $object;
    foreach ($object as $key => $val) {
        $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;
        $result[$key] = $val;
    }
    return $result;
}

function C($name=null, $value=null,$default=null) {
    static $_config = array();
    // 无参数时获取所有
    if (empty($name)) {
        return $_config;
    }
    // 优先执行设置获取或赋值
    if (is_string($name)) {
        if (!strpos($name, '.')) {
            $name = strtolower($name);
            if (is_null($value))
                return isset($_config[$name]) ? $_config[$name] : $default;
            $_config[$name] = $value;
            return;
        }
        // 二维数组设置和获取支持
        $name = explode('.', $name);
        $name[0]   =  strtolower($name[0]);
        if (is_null($value))
            return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : $default;
        $_config[$name[0]][$name[1]] = $value;
        return;
    }
    // 批量设置
    if (is_array($name)){
        $_config = array_merge($_config, array_change_key_case($name));
        return;
    }
    return null; // 避免非法参数
}

function http_request($param)
{
    $url = $param['url'];
    $data = !empty($param['data']) ? $param['data'] : '';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if ($data){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    //return json_decode($output,TRUE);
    return $output;
}
//创建文件夹
function sy_mkdir($absolute_path, $mode = 0777)
{
    if (is_dir($absolute_path))
    {
        return true;
    }

    $root_path      = ROOTPATH;
    $relative_path  = str_replace($root_path, '', $absolute_path);
    $each_path      = explode('/', $relative_path);
    $cur_path       = $root_path; // 当前循环处理的路径
    foreach ($each_path as $path)
    {
        if ($path)
        {
            $cur_path = $cur_path . '/' . $path;
            if (!is_dir($cur_path))
            {
                if (@mkdir($cur_path, $mode))
                {
                    fclose(fopen($cur_path . '/index.html', 'w'));
                }
                else
                {
                    return false;
                }
            }
        }
    }

    return true;
}



